package com.crm.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.crm.domain.ControllerLog;
import com.crm.crm.domain.dto.PageQueryParams;
import com.crm.crm.domain.vo.ControllerLogVo;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.mapper.LogMapper;
import com.crm.crm.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, ControllerLog> implements LogService {
    @Autowired
    private LogMapper  logMapper;
    @Override
    public PageResult<ControllerLogVo> findLogs(PageQueryParams pageQueryParams) {
        Page<ControllerLog> page = new Page<>(pageQueryParams.getPageNum(),pageQueryParams.getPageSize());
        LambdaQueryWrapper<ControllerLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(pageQueryParams.getKeyWord()),ControllerLog::getModule,pageQueryParams.getKeyWord());

        if (StringUtils.isNotEmpty(pageQueryParams.getBeginCreateTime())&&StringUtils.isNotEmpty(pageQueryParams.getEndCreateTime())) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate  begin = LocalDate.parse(pageQueryParams.getBeginCreateTime(),dateTimeFormatter);
            LocalDate  end = LocalDate.parse(pageQueryParams.getEndCreateTime(),dateTimeFormatter);
            queryWrapper.between(ControllerLog::getOperationTime,begin,end);
        }
        Page<ControllerLog> pageResult = this.page(page,queryWrapper);
        List<ControllerLogVo> controllerLogVos = pageResult.getRecords().stream().map(log -> {
            String[] str = log.getUserInfo().split("/");
            String ip = str[1];
            String operName = str[0];
            ControllerLogVo controllerLogVo = new ControllerLogVo();
            controllerLogVo.setOperId(Math.toIntExact(log.getId()));
            controllerLogVo.setTitle(log.getModule());
            controllerLogVo.setBusinessType(null);
            controllerLogVo.setBusinessTypes(null);
            controllerLogVo.setMethod(log.getClassMethod());
            controllerLogVo.setRequestMethod(log.getRequestMethod());
            controllerLogVo.setOperatorType(null);


            controllerLogVo.setOperName(operName);
            controllerLogVo.setDeptName(null);
            controllerLogVo.setOperUrl(log.getRequestUrl());

            controllerLogVo.setOperIp(ip);
            controllerLogVo.setOperLocation(null);
            controllerLogVo.setOperParam(log.getRequestParams());
            controllerLogVo.setJsonResult(log.getResponseData());

            controllerLogVo.setStatus("正常".equals(log.getStatus()) ? 0:1);
            controllerLogVo.setErrorMsg(null);
            controllerLogVo.setOperTime(log.getOperationTime().toString().replace("T", " "));

            return controllerLogVo;
        }).collect(Collectors.toList());

        PageResult<ControllerLogVo> result = new PageResult<>(pageResult.getTotal(), controllerLogVos);

        return result;
    }

    @Override
    public void truncateTable() {
        String tableName = "controller_log"; // 替换成你的表名
        String sql = "TRUNCATE TABLE " + tableName;
        logMapper.executeSql(sql);



        // 如果是 MySQL，重置自增 ID
        String resetSql = "ALTER TABLE " + tableName + " AUTO_INCREMENT = 1";
        logMapper.executeSql(resetSql);

    }


}
